import { Base64 } from 'js-base64';
import { observable, action, toJS } from 'mobx';
import { Bubble, Toast, Tools } from 'weaver-mobile';
import { ListView } from 'weaver-mobile';
import Api from '../../apis/view';

const getLabel = Tools.getLabel;
const initalState = {
  staticViewBean: {},
  staticMailContent: '',
  staticViewRight: 0,
  staticIsSimple: 0,
  iframContent: false,
  iframContentLoading: true,
  readTopList: [],
  staticLoading: true,
  staticBaseLoading: true,
}
export default class StaticViewStore {
  path;
  @observable staticLoading = initalState.staticLoading;
  @observable staticBaseLoading = initalState.staticBaseLoading;
  @observable staticViewRight = initalState.staticViewRight; // 查看权限
  @observable staticViewBean = initalState.staticViewBean;
  @observable staticMailContent = initalState.staticMailContent;
  @observable staticIsSimple = initalState.staticIsSimple;
  @observable iframContent = initalState.iframContent;
  @observable iframContentLoading = initalState.iframContentLoading;
  @observable readTopList = initalState.readTopList;  // 查看所有发件人 && 收件人信息
  @observable staticFromList = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 });
  @observable staticToList = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 });
  @observable staticCopyList = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 });
  @observable staticSecretList = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 });

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  @action clear = () => this.setStoreState(initalState);

  @action getViewRight = (params) => {
    this.staticLoading = true;
    Api.getRight(params).then(res => {
      this.staticViewRight = res.viewRight;
      this.iframContentLoading = true;
      if (res.viewRight * 1 === 1) {
        this.getBase(params);
        this.getContent(params);
      } 
      this.staticLoading = false;
    });
  }

  @action getBase = (params) => {
    this.staticBaseLoading = true;
    Api.getBase(params).then(res => {  // 获取邮件基本信息
      if (res.viewRight * 1 === 1 && res.status * 1 === 1) {
        this.staticViewBean = res.viewBean;
        this.staticIsSimple = res.viewBean.showmode;
      }
      this.staticBaseLoading = false
    })
  }

  @action getContent = (params) => {
    Api.getContent(params).then(res => {  // 获取邮件内容信息
      if (res.viewRight * 1 === 1 && res.status * 1 === 1) {
        this.staticMailContent = Base64.decode(res.mailContent);
          // 情况：1、内容本身为空  2、纯文本内容  3、带有图片的邮件
        this.iframContent = this.staticMailContent.match(/<img.*?(?:>|\/>)/gi);  // 这封邮件本身就没有图片 返回为null, imgReg.length——>报错
        this.iframContentLoading = false;
      }
    })
  }

  @action getReadList = () => {
    const { fromSpan = {}, toSpan = { list: [] }, ccSpan = { list: [] }, bccSpan = { list: [] } } = toJS(this.staticViewBean);
    this.readTopList = [];
    if(fromSpan) {
			this.readTopList.push({
        label: getLabel(2034, '发件人'),
        count: 1,
        key: 'sendfrom'
			});
			let fromSpanList = [];
			fromSpanList.push(fromSpan);
      this.staticFromList = this.staticFromList.cloneWithRows(fromSpanList);
    }
    if (toSpan.list.length > 0) {
      this.readTopList.push({
        label: getLabel(2046, '收件人'),
        count: toSpan.list.length,
        key: 'sendto'
      });
      this.staticToList = this.staticToList.cloneWithRows(toSpan.list);
    }
    if (ccSpan.list.length > 0) {
      this.readTopList.push({
        label: getLabel(17051, '抄送人'),
        count: ccSpan.list.length,
        key: 'sendcc'
      });
      this.staticCopyList = this.staticCopyList.cloneWithRows(ccSpan.list);
    }
    if (bccSpan.list.length > 0) {
      this.readTopList.push({
        label: getLabel(81316, '密送人'),
        count: bccSpan.list.length,
        key: 'sendbcc'
      });
      this.staticSecretList = this.staticSecretList.cloneWithRows(bccSpan.list);
    }
  }
} 